/**
 * Copyright © 2011 Software in Life Inc. All rights reserved.
 */
package com.softwareinlife.snaf.user.rest;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.annotations.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.softwareinlife.snaf.user.dao.UserDao;
import com.softwareinlife.snaf.user.model.UserModel;

/**
 * <b>RESTful Web Service Component for Users</b>
 * 
 * <p>
 * For a complete reference see <a href=
 * "http://jersey.java.net/nonav/documentation/latest/index.html" > Jersey 1.6
 * User Guide Documentations</a>
 * </p>
 * 
 * @author Jang, Sun-Jin(jangsunjin@softwareinlife.com)
 */
@Path("/user")
@Component
@Scope("singleton")
public class UserRest {

	/**
	 * Logger
	 */
	protected static Logger logger = Logger.getLogger("rest");

	/**
	 * DAO
	 */
	@Autowired
	private UserDao dao;

	/**
	 * Get
	 * 
	 * @return
	 */
	@GET
	@Path("/{id}")
	@Transactional
	@Produces({ "application/xml", "application/json" })
	@SuppressWarnings("unchecked")
	public UserModel get(@PathParam("id") String id) {
		// Select Model
		UserModel model = dao.read(id);

		return model;
	}

	/**
	 * Get
	 * 
	 * @return
	 */
	@GET
	@Transactional
	@Produces({ "application/xml", "application/json" })
	@SuppressWarnings("unchecked")
	public List<UserModel> get() {
		// Select Model
		List<UserModel> model = null;

		// Select Model
		model = dao.getModels();

		return model;
	}

	/**
	 * Get
	 * 
	 * @return
	 */
	@POST
	@Transactional
	@Consumes({ "application/xml", "application/json" })
	@Produces({ "application/xml", "application/json" })
	@SuppressWarnings("unchecked")
	public UserModel post(@ModelAttribute UserModel model) {
		// Create Model
		dao.create(model);

		// Return Result
		return model;
	}

	/**
	 * Get
	 * 
	 * @return
	 */
	@GET
	@Path("/test")
	@Transactional
	@Produces({ "application/xml", "application/json" })
	@SuppressWarnings("unchecked")
	public UserModel test() {
		// Select Model
		UserModel model = new UserModel("v");

		dao.create(model);

		return model;
	}
}
